import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;


import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.NORTH;
import static java.awt.BorderLayout.SOUTH;


/**
 * Created by blatand on 2017/2/10.
 */
public class TalkClient {

    BitMapShowDemo showBitMap;
    private JButton bt = new JButton("Analyse8583");
    JButton bt2 = new JButton("SetBitMap");

    private JTextArea t1;
    private JTextArea t2;



    public TalkClient(JFrame frame){


        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        t1 = new JTextArea();
        t2 = new JTextArea();
        contentPane.add(showMsgTest(t1,"原始报文(BITMAP+MSG)",null),NORTH);
        contentPane.add(showMsgTest(t2,"解析后报文",null),CENTER);
        contentPane.add(showButton(bt,bt2),SOUTH);

        bt2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(showBitMap==null){
                    showBitMap = new BitMapShowDemo();
                    showBitMap.InitSelectBitMap();
                }
                showBitMap.show();
            }
        });
        bt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String InputMsg = t1.getText().replaceAll("\\s","").replaceAll("\n","");
                if(!InputMsg.isEmpty()){
                    String DisMsg = ActionMethod(InputMsg);
                    if(DisMsg.isEmpty()){
                        t2.setText("OutPut File error");
                    }else{
                        t2.setText(DisMsg);
                    }
                }
            }
        });
        t1.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {

            }
            @Override
            public void removeUpdate(DocumentEvent e) {

            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(0,0);
        frame.setSize(screenSize.width,screenSize.height-100);
//        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    public static JPanel showMsgTest(JTextArea t,String title,String text){

        JScrollPane scrollBar;
        JPanel jpanel = new JPanel();
        jpanel.setLayout(new BorderLayout());
        JLabel lb = new JLabel(title);
        lb.setFont(new Font("TimesRoman",Font.BOLD,20));
        t.setFont(new Font("TimesRoman",Font.BOLD,20));
        t.setColumns(10);
        t.setRows(10);
        t.setText(text);

        scrollBar = new JScrollPane(t);

        jpanel.add(lb,NORTH);
        jpanel.add(scrollBar,CENTER);

        return jpanel;
    }
    public static String ReadInputFile(String filePath) {

        File file = new File(filePath);

        StringBuilder sb = new StringBuilder();
        String s = new String();

        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            while( (s = br.readLine())!=null) {

                sb.append(s);
                sb.append("\n");
            }
            br.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String args[]){

        JFrame frame = new JFrame("ToolForParse8583");
        TalkClient i = new TalkClient(frame);
        frame.setVisible(true);
    }

    public static String ActionMethod(String msg){


        File outPutFile = new File("parseMsgOutput.txt");
        if(true){

            Msg8583Util input8583bag = Msg8583Util.Unpackbag(
                    ToolUtil.sdkAscToBcd(msg.getBytes()),
                    "bitmap.ini");

            DataSaveUtil.SaveParseMsg(input8583bag,"tradeLog.txt","parseMsgOutput.txt",DataSaveUtil.FILE);
        }
        return ReadInputFile(outPutFile.getPath());
    }
    public static JPanel showButton(JButton bt1,JButton bt2){

        JPanel jpanel = new JPanel();
        jpanel.setLayout(new FlowLayout());
        jpanel.add(bt1);
        jpanel.add(bt2);
        return jpanel;
    }

}
